package cn.edu.scnu.controller;

import cn.edu.scnu.entity.User;
import cn.edu.scnu.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class VipController {
    @Autowired
    UserService userService;

    //充值页面
    @RequestMapping("/vip")
    public String vip() {
        return "vip";
    }

    //检查当前登录用户是否是VIP会员
    @RequestMapping("/isVip")
    @ResponseBody
    public Boolean isVip(HttpSession httpSession) {
        User loginUser = (User) httpSession.getAttribute("userLogin");
        System.out.println(loginUser);
        if (loginUser != null && loginUser.getIsVip() == 1)
            return true;
        else
            return false;
    }

    //模拟支付成功后更新用户的VIP状态
    @RequestMapping("/paySuccess")
    @ResponseBody
    public Boolean paySuccess(HttpSession httpSession) {
        User loginedUser = new User();
        loginedUser = (User) httpSession.getAttribute("user");
        if(loginedUser!=null) {
            loginedUser.setIsVip(1);
            userService.updateUser(loginedUser);
            httpSession.removeAttribute("user");
            httpSession.setAttribute("user", loginedUser);
        }
        return true;
    }
}